<?php
require_once('scripts/Message.php');
require_once('scripts/twitter/twitter.php');

class TwitterMessage extends Message{
	protected $receiverTwitterName;
	protected $privateMessage;
	protected $result;
	protected $twitterObject;
	protected $twitterMessage;

	function __construct($messageId){
		parent::__construct($messageId);

		$this->twitterObject		= new Twitter();
		$this->twitterObject->setOAuthToken();
		$this->twitterObject->setOAuthTokenSecret();

		$this->fetchTwitterName();
		$this->checkPrivate();
		$this->toTwitter();
	}

	function fetchTwitterName(){
		$query				= "SELECT TwitterName FROM Twitter WHERE PeId = ".$this->receiverPeId;
		$result				= $this->db->query($query);
		$row				= $result->fetch_assoc();

		$this->receiverTwitterName	= $row['TwitterName'];
	}

	function checkPrivate(){
		if ($this->twitterObject->friendshipsExists('hubmsg', $this->receiverTwitterName) == 1
			&& $this->twitterObject->friendshipsExists($this->receiverTwitterName, 'hubmsg') == 1){
			$this->privateMessage	= true;
		} else {
			$this->privateMessage	= false;
		}
	}

	function toTwitter(){
                //generates the shorten message for Twitter

		//set the variables for this function
		$twitterId			= $this->receiverTwitterName;
		$senderName			= $this->senderName;
		$senderFirstName		= $this->senderFirstName;
		$message			= $this->message;
		$messageTitle			= $this->messageTitle;
		$direct				= $this->privateMessage;

		$namestr			= $senderFirstName." ".$senderName;
		$smallHint			= " Would like to contact you. Please check your email!";

		$namelen			= strlen($namestr);    //how long is the name
		$hintlen			= strlen(" Would like to contact you on the subject of '', please check your email!");
		$messagelen			= strlen($message);
		$smallHintLen			= strlen($smallHint);

		if ($direct == true){

			/*
			#=================================================#
			#     We're going to send a tweet as mention.     #
			#=================================================#
			*/

			if ( $namelen + $messagelen + 7 <= 140){	//Send complete message

				$shortmessage	= $namestr." says: ".$message;

			} else if ( $namelen + $hintlen + 10 <= 140 ){	//send hint with theme if at least 10 characters of the title fit

				//calculate how long the title can be
				$maxTitleLen	= 140 - $namelen - $hintlen;
				$titleSubStr	= substr($messageTitle, 0, $maxTitleLen );
				$shortmessage	= $namestr." Would like to contact you on the subject of '".$titleSubStr."', please check your email!";

			} else if ( $namelen + $smallHintLen <= 140) {	//send hint without theme

				$shortmessage	= $namestr.$smallHint;

			} else {

				$initials	= substr($senderFirstName, 0, 1).".".substr($senderName,0 ,1).".";
				$shortmessage	= $initials.$smallHint;

			}

		} else {

			/*
			#=================================================#
			#      We're going to send a tweet as mention     #
			#=================================================#
			*/

			$twitterIdLen		= strlen($twitterId);
			//$smallHintStrLen = strlen(" Would like to contact you. Please check your email!");

			If ( $twitterIdLen + +$namelen + $messagelen + 9 <= 140){	//send complete message

				$shortmessage	= "@".$twitterId." ".$namestr." says: ".$message;

			} else if ( $twitterIdLen + $namelen + $hintlen + 12 <= 140 ){

				//calculate how long the title can be
                                $maxTitleLen	= 140 - $twitterIdLen - $namelen - $hintlen -2;
				$titleSubStr	= substr($messageTitle, 0, $maxTitleLen);
				$shortmessage	= "@".$twitterId." ".$namestr." Would like to contact you on the subject of '".$titleSubStr."', please check your email!";

			} else if ( $twitterIdLen + $namelen + $smallHintLen + 2 <= 140){

				$shortmessage	= "@".$twitterId." ".$namestr.$smallHint;

			} else {

				$initials	= substr($senderFirstName, 0, 1).".".substr($senderName,0 ,1).".";
				$shortmessage	= "@".$twitterId." ".$initials.$smallHint;

			}

		}

		$this->twitterMessage		= $shortmessage;
                //return($shortmessage);
	}

	public function sendMessage(){
		$message 			= $this->twitterMessage;
		$receiverTwitterName		= $this->receiverTwitterName;

		if ($this->privateMessage == true){
			$response		= $this->twitterObject->directMessagesNew($message, $receiverTwitterName);
		} else {
			$response		= $this->twitterObject->statusesUpdate($message);
		}
	}

	public function getReceiverTwitterName(){
		return $this->receiverTwitterName;
	}
	public function getPrivateMessage(){
		return $this->privateMessage;
	}
	public function getResult(){
		return $this->result;
	}
	public function getTwitterMessage(){
		return $this->twitterMessage;
	}
}
?>
